package com.kr.weitao.model;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import com.kr.weitao.util.MD5Util;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Created by lide on 2017/6/5.
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@JSONType
public class DataRequest {
    @JSONField
    @ApiModelProperty(value = "请求唯一ID")
    private String id;

    @JSONField
    @ApiModelProperty(value = "请求方法")
    private String method;

    @JSONField
    @ApiModelProperty(value = "请求来源")
    private Source source = Source.WHALE;

    @JSONField
    @ApiModelProperty(value = "请求数据体")
    private JSONObject data = new JSONObject();

    @JSONField
    @ApiModelProperty(value = "加密字段")
    private String access_key;

    @JSONField
    @ApiModelProperty(value = "加密体")
    private String sign;

    @JSONField
    @ApiModelProperty(value = "时间戳")
    private String timestamp = String.valueOf(System.currentTimeMillis());

    public DataRequest setMethod(String method) {
        this.method = method;
        return this;
    }

    public DataRequest setSource(Source source) {
        this.source = source;
        return this;
    }

    public DataRequest setData(JSONObject data) {
        this.data = data;
        return this;
    }

    public DataRequest setAccess_key(String accessKey) {
        this.access_key = accessKey;
        return this;
    }

    public DataRequest setSign(String sign) {
        this.sign = sign;
        return this;
    }

    public DataRequest setTimestamp(String timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    /**
     * 判断请求来源
     *
     * @return 本公司：true；第三方：false；
     */
    public boolean isInnerSource() {
        return getSource().equals(Source.WHALE) ? true : false;
    }

    /**
     * 获取请求签名
     *
     * @param accessKey
     * @param secret
     * @return
     */
    public String getSign(String accessKey, String secret) {
        String sign = MD5Util.getMD5Str32(accessKey + secret + timestamp).toUpperCase();
        return sign;
    }

}
